package com.lizheng.yunservice.travel.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.lizheng.yunservice.common.domain.UserInfo;
import com.lizheng.yunservice.common.domain.ApiRestResponse;
import com.lizheng.yunservice.common.domain.PageQuery;
import com.lizheng.yunservice.travel.doamin.vo.VtpCustomerLeaveMsgPageResponse;
import com.lizheng.yunservice.travel.doamin.vo.VtpCustomerLeaveMsgVo;
import com.lizheng.yunservice.travel.service.IVtpCustomerLeaveMsgService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lizheng.yunservice.common.page.TableDataInfo;
import com.lizheng.yunservice.travel.dto.VtpCustomerLeaveMsgDto;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 客户留言
 *
 * @author lizheng
 * &#064;date  2024-06-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/vtp/app/customerLeaveMsg/")
public class VtpAppCustomerLeaveMsgController {

    private final IVtpCustomerLeaveMsgService vtpCustomerLeaveMsgService;

    /**
     * 查询物产前10条留言列表
     */
    @GetMapping("/getGoodsTop10List/{goodsId}")
    public ApiRestResponse<List<VtpCustomerLeaveMsgVo>> getGoodsTop10List(@PathVariable String goodsId, HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        List<VtpCustomerLeaveMsgVo> list = vtpCustomerLeaveMsgService.getGoodsTop10List(goodsId, authorization);
        return ApiRestResponse.ok(list);
    }

    /**
     * 查询物产前2条留言列表
     */
    @GetMapping("/getGoodsTop2List/{goodsId}")
    public ApiRestResponse<List<VtpCustomerLeaveMsgVo>> getGoodsTop2List(@PathVariable String goodsId, HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        List<VtpCustomerLeaveMsgVo> list = vtpCustomerLeaveMsgService.getGoodsTop2List(goodsId, authorization);
        return ApiRestResponse.ok(list);
    }

    /**
     * 查询商品更多留言分页列表
     */
    @GetMapping("/list")
    public ApiRestResponse<VtpCustomerLeaveMsgPageResponse> getGoodsMoreList(@RequestParam(required = false) Long objectId,
                                                                             @RequestParam(required = false) String objectType,
                                                                             PageQuery pageQuery,
                                                                             HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        VtpCustomerLeaveMsgPageResponse response = vtpCustomerLeaveMsgService.getGoodsMoreList(objectId, objectType, pageQuery, authorization);
        return ApiRestResponse.ok(response);
    }

    /**
     * 新增留言
     */
    @SaCheckLogin
    @PostMapping("/save")
    public ApiRestResponse<?> saveLeaveMsg(@RequestBody VtpCustomerLeaveMsgDto dto) {
        try {
            // 安全获取用户信息
            if (!StpUtil.isLogin()) {
                return ApiRestResponse.fail(401, "用户未登录或登录已过期");
            }

            // 从登录会话中获取完整的用户信息
            UserInfo userInfo = (UserInfo) StpUtil.getSession().get("userInfo");

            if (userInfo == null) {
                return ApiRestResponse.fail(401, "无法获取用户信息");
            }

            // 保存留言
            int result = vtpCustomerLeaveMsgService.saveLeaveMsg(
                    dto,
                    userInfo.getUserId(),
                    userInfo.getNickName(),
                    userInfo.getAvatar()
            );

            return result > 0
                    ? ApiRestResponse.ok()
                    : ApiRestResponse.fail("留言保存失败");
        } catch (IllegalArgumentException e) {
            return ApiRestResponse.fail(400, e.getMessage());
        } catch (Exception e) {
            return ApiRestResponse.fail(500, "系统错误：" + e.getMessage());
        }
    }

    @SaCheckLogin
    @GetMapping("/platformList")
    public ApiRestResponse<TableDataInfo<VtpCustomerLeaveMsgVo>> getPlatformList(
            @RequestParam(required = false) String objectType,
            PageQuery pageQuery) {

        try {
            // 直接使用Sa-Token获取用户信息，不再需要手动处理Authorization头
            TableDataInfo<VtpCustomerLeaveMsgVo> response = vtpCustomerLeaveMsgService.getPlatformList(objectType, pageQuery);
            return ApiRestResponse.ok(response);
        } catch (IllegalArgumentException e) {
            return ApiRestResponse.fail(400, e.getMessage());
        } catch (Exception e) {
            return ApiRestResponse.fail(500, "系统错误，请稍后重试");
        }
    }
}